//
//  FindViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "FindViewController.h"
#import "ManyPeopleListenCollectionViewCell.h"
#import "ManyPeopleListenCollectionReusableView.h"
#import "CustomerNetworking.h"
#import "ManyPeopleListenModel.h"
#import "ListenDataModel.h"
#import "HotSongListCollectionViewCell.h"
#import "NewSongCollectionViewCell.h"
#import "NewMVCollectionViewCell.h"
#import "TheBestHotTVCollectionViewCell.h"
#import "EveryDayCollectionViewCell.h"
#import "TheNewSongsViewController.h"
#import "ManyListenListViewController.h"
#import "HotListenListViewController.h"
#import "TheNewChoseViewController.h"
#import "RecycleScrollCollectionReusableView.h"
#import "TheNewMVViewController.h"
#import "TheNewMVModel.h"
#import "AVPlayerViewController.h"
#import "MvListModel.h"
#import "BaseClassNavigationController.h"
#import <MJRefresh.h>
#import "PlayerViewController.h"
#import "SongsAuditionListModel.h"
#import "MusicType.h"
@interface FindViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, ManyPeopleListenCollectionReusableViewDelegate, RecycleScrollCollectionReusableViewDelegate>
@property(nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, retain) NSMutableArray *dataArray;
@property(nonatomic, retain) UINavigationBar *bar;
@property(nonatomic, retain) NSMutableArray *picArray;
@end

@implementation FindViewController
- (void)dealloc {
    [_collectionView release];
    [_dataArray release];
    [_bar release];
    [_picArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.picArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    //创建collectionView
    [self createCollectionView];
    //获取数据
    [self loadData];
    [self loadNewData];
}
- (void)loadNewData {
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray  removeAllObjects];
        [self loadData];
    }];
}
#pragma mark 获取数据
-(void)loadData {
    [CustomerNetworking customerConnectWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447245006&app=ttpod&v=v8.1.1.2015110616" andParameters:nil andBlock:^(id response) {
        NSArray *array = [response valueForKey:@"data"];
        if (array.count != 0) {
            for (int i = 0; i < array.count - 3; i++) {
                if (i != 1 && i != 5 && i != 6) {
                    ManyPeopleListenModel *model = [[ManyPeopleListenModel alloc]initWithDictionary:array[i]];
                    [_dataArray addObject:model];
                    [model release];
                }
            }
        }
        [self.collectionView.header endRefreshing];
        [self.collectionView reloadData];
    }];
}
#pragma mark 创建collectionView
-(void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 120) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    //注册第一个cell重用标识
    [_collectionView registerClass:[ManyPeopleListenCollectionViewCell class] forCellWithReuseIdentifier:@"manyPeopleListenCollectionViewCellIdentify"];
    //注册第二个cell重用标识
    [_collectionView registerClass:[HotSongListCollectionViewCell class] forCellWithReuseIdentifier:@"HotSongListCollectionViewCellIdentify"];
    //注册第三个cell重用标识
    [_collectionView registerClass:[NewSongCollectionViewCell class] forCellWithReuseIdentifier:@"NewSongCollectionViewCellIdentify"];
    //注册第四个cell重用标识
    [_collectionView registerClass:[NewMVCollectionViewCell class] forCellWithReuseIdentifier:@"NewMVCollectionViewCellIdentify"];
    //注册第五个cell重用标识
    [_collectionView registerClass:[TheBestHotTVCollectionViewCell class] forCellWithReuseIdentifier:@"TheBestHotTVCollectionViewCellIdentify"];
    //注册第六个cell重用标识
    [_collectionView registerClass:[EveryDayCollectionViewCell class] forCellWithReuseIdentifier:@"EveryDayCollectionViewCellIdentify"];
    //注册第一个头视图
    [_collectionView registerClass:[RecycleScrollCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerOne"];
    //注册第二个头视图
    [_collectionView registerClass:[ManyPeopleListenCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerTow"];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        ManyPeopleListenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"manyPeopleListenCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.dataSongsModel = [[[[self.dataArray[1] dataArray] objectAtIndex:0] songArray] objectAtIndex:indexPath.row];
        cell.queueString = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        return cell;
    }else if (indexPath.section == 2 || indexPath.section == 3 ) {
        HotSongListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotSongListCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.listenDataModel = [[self.dataArray[indexPath.section] dataArray] objectAtIndex:indexPath.row];
        return cell;
    }else if (indexPath.section == 4 ) {
        NewSongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewSongCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.listenDataModel = [[self.dataArray[indexPath.section] dataArray] objectAtIndex:indexPath.row];
        return cell;
    }else if (indexPath.section == 5) {
        NewMVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewMVCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.listenDataModel = [[self.dataArray[indexPath.section] dataArray] objectAtIndex:indexPath.row];
        return cell;
    }else if (indexPath.section == 6){
        TheBestHotTVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TheBestHotTVCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.listenDataModel = [[self.dataArray[indexPath.section] dataArray] objectAtIndex:indexPath.row];
        return cell;
    }else {
        EveryDayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EveryDayCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.listenDataModel = [[self.dataArray[indexPath.section] dataArray] objectAtIndex:indexPath.row];
        return cell;
    }
}
#pragma mark 头尾视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            RecycleScrollCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerOne" forIndexPath:indexPath];
            headerView.delegate = self;
            if ([_dataArray[indexPath.section]dataArray].count != 0) {
                NSMutableArray *array = [_dataArray[indexPath.section]dataArray];
                for (int i = 0 ; i < array.count; i++) {
                    if (![[[array[i] actionDic] valueForKey:@"type"] isEqualToNumber:[NSNumber numberWithInt:5]] ) {
                        [array removeObject:array[i]];
                    }
                }
                headerView.picArray = array;
            }
            return headerView;
        }else {
            ManyPeopleListenCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerTow" forIndexPath:indexPath];
            headerView.delegate = self;
            headerView.manyPeopleListenModel = [_dataArray objectAtIndex:indexPath.section];
            headerView.index = [NSString stringWithFormat:@"%ld", indexPath.section];
            if (indexPath.section == 1 || indexPath.section == 6) {
                headerView.showDesc = YES;
            }else {
                headerView.showDesc = NO;
            }
            if (indexPath.section == 1  || indexPath.section == 4 || indexPath.section == 5) {
                headerView.showButton = YES;
            } else {
                headerView.showButton = NO;
            }
            return headerView;
        }
    }else {
        return  nil;
    }
    
}
#pragma mark cell的大小
-(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(0, 0);
    }else if (indexPath.section == 1) {
        return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height / 7);
    }else if (indexPath.section == 6 || indexPath.section == 7){
        return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height / 5);
    }else{
        return CGSizeMake((self.view.frame.size.width - 40) / 3, self.view.frame.size.height / 4);
    }
}
#pragma mark 头视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(self.view.frame.size.width, 200);
    }else {
        return CGSizeMake(self.view.frame.size.width, 50);
    }
}
#pragma mark cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return [[[self.dataArray[1] dataArray] objectAtIndex:0] songArray].count;
    }else{
        return [self.dataArray[section] dataArray].count;
    }
}
#pragma mark 分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return  _dataArray.count ? _dataArray.count : 0;
}
#pragma mark cell之间的最小间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}
#pragma mark cell的最小行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1) {
        return 5;
    }else {
        return 10;
    }
}
#pragma mark cell的上下左右的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 1 ) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else {
        return UIEdgeInsetsMake(0, 10, 5, 10);
    }
}
#pragma mark 单元格点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 7) {
        HotListenListViewController *view = [[HotListenListViewController alloc]init];
        view.idString = [[[[[_dataArray objectAtIndex:indexPath.section] dataArray] objectAtIndex:indexPath.item] actionDic] valueForKey:@"value"];
        view.numberString = @"http://api.songlist.ttpod.com/songlists/";
        [self.navigationController pushViewController:view animated:YES];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [view release];
    }else if (indexPath.section == 6) {
        HotListenListViewController *view = [[HotListenListViewController alloc]init];
        view.idString = [[[_dataArray objectAtIndex:indexPath.section] actionDic] valueForKey:@"value"];
        view.numberString = @"http://api.songlist.ttpod.com/songlists/";
        [self.navigationController pushViewController:view animated:YES];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [view release];
    }else if (indexPath.section == 4) {
        if ([[[[[[_dataArray objectAtIndex:indexPath.section] dataArray] objectAtIndex:indexPath.item] actionDic] valueForKey:@"type"] isEqualToNumber:[NSNumber numberWithInt:19]]) {
            HotListenListViewController *view = [[HotListenListViewController alloc]init];
            view.idString = [[[[[_dataArray objectAtIndex:indexPath.section] dataArray] objectAtIndex:indexPath.item] actionDic] valueForKey:@"value"];
            view.numberString = @"http://api.dongting.com/song/album/";
            [self.navigationController pushViewController:view animated:YES];
            [self.navigationController setNavigationBarHidden:YES animated:YES];
            [view release];
        }else {
            TheNewChoseViewController *view = [[TheNewChoseViewController alloc]init];
            [self.navigationController pushViewController:view animated:YES];
            [self.navigationController setNavigationBarHidden:YES animated:YES];
            [view release];
        }
    }else if (indexPath.section == 5) {
        NSString *string = [[[[[_dataArray objectAtIndex:indexPath.section] dataArray] objectAtIndex:indexPath.item] actionDic] valueForKey:@"value"];
        [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.dongting.com/song/video/%@?app=ttpod&v=v8.1.1.2015110616", string] andParameters:nil andBlock:^(id response) {
            TheNewMVModel *model = [[TheNewMVModel alloc]initWithDictionary:[response valueForKey:@"data"]];
            AVPlayerViewController *view = [[AVPlayerViewController alloc]init];
            view.idString = [model.mvList.lastObject videoId];
            view.urlString = [model.mvList.lastObject urlStr];
            BaseClassNavigationController *navgationController = [[BaseClassNavigationController alloc] initWithRootViewController:view];
            [navgationController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsCompact];
            [self presentViewController:navgationController animated:YES completion:^{
                
            }];
            [navgationController release];
            [view release];
            [model release];
        }];
    }else if (indexPath.section == 1) {
        ListenDataSongsModel *song =[[[[self.dataArray[indexPath.section] dataArray] lastObject] songArray] objectAtIndex:indexPath.row];
        PlayerViewController *player = [PlayerViewController sharePlayerViewController];
        NSMutableArray *arr = [NSMutableArray array];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:song.albumName forKey:@"album"];// 专辑
        [dic setValue:song.name forKey:@"name"]; // 歌名
        NSMutableArray *array = [NSMutableArray array];
        for (SongsAuditionListModel *model in song.auditionList) {
            MusicType *type = [[MusicType alloc]init];
            type.url = model.urlString;
            type.typeDescription = model.typeDescription;
            [array addObject:type];
        }
        [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
        [dic setValue:song.singerName forKey:@"songerName"];// 歌手名
        [dic setValue:song.albumId forKey:@"id"]; // id
        [arr addObject:dic];
        player.musicArr = arr;
        [player MusicStart:0];
                   }
    
}
#pragma mark 头视图的代理方法
-(void)moreButtonClickedAction:(NSString *)index {
    if ([index isEqualToString:@"4"]) {
        TheNewSongsViewController *view = [[TheNewSongsViewController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }else if ([index isEqualToString:@"1"]) {
        ManyListenListViewController *view = [[ManyListenListViewController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [view release];
    }else if ([index isEqualToString:@"5"]) {
        TheNewMVViewController *view = [[TheNewMVViewController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }
}
#pragma mark 轮播图的点击事件
-(void)recycleScrollViewClickedAction:(ListenDataModel *)model {
    HotListenListViewController *view = [[HotListenListViewController alloc]init];
    view.idString = [model.actionDic valueForKey:@"value"];
    view.numberString = @"http://api.songlist.ttpod.com/songlists/";
    [self.navigationController pushViewController:view animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [view release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
